﻿namespace VideoWorld.controls
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.Security;

    // 07301061
    // 董敬超
    // 2010-6-4
    public class login : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.TextBox testCode;
        protected System.Web.UI.WebControls.Image Image1;
        private String newError, url;
        protected System.Web.UI.WebControls.TextBox name;
        protected System.Web.UI.WebControls.TextBox password;
        protected System.Web.UI.WebControls.Button regTrue;
        protected System.Web.UI.WebControls.Button reg;
        protected System.Web.UI.WebControls.Button findPasswd;
        DataAccess.Users users = new DataAccess.Users(Global.ConnectionString);

        private void Page_Load(object sender, System.EventArgs e)
        {
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.regTrue.Click += new System.EventHandler(this.regTrue_Click);
            this.reg.Click += new System.EventHandler(this.reg_Click);
            this.findPasswd.Click += new System.EventHandler(this.findPasswd_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        //用户注册
        private void regTrue_Click(object sender, System.EventArgs e)
        {
            Check();
            UserLogin();
        }
        //跳转到新用户注册页面
        private void reg_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("UserProtocol.aspx");
        }
        //跳转到找回密码页面
        private void findPasswd_Click(object sender, System.EventArgs e)
        {
            Response.Redirect("GetPassword.aspx");
        }
        //检查用户输入格式
        private void Check()
        {
            if (!Business.Users.IsValidEmail(name.Text))
            {
                newError = String.Format("帐号是E-mail格式!");
                url = String.Format("login.aspx?error={0}", newError);
                Page.Response.Redirect(url);
            }

            if (Business.Users.VerifyText(password.Text))
            {
                newError = String.Format("请输入密码!");
                url = String.Format("login.aspx?error={0}", newError);
                Page.Response.Redirect(url);
            }
        }
        //用户登录方法
        private void UserLogin()
        {
            String resultMessage = users.IsUserLogin(name.Text, password.Text);
            if (resultMessage != "ok")
            {
                newError = resultMessage;
                Session["userName"] = null;
                if (resultMessage == "您的充值卡已经过期,或者没有购买充值，请购买新的充值卡进行充值!")
                {
                    Session["userName"] = name.Text;//成功登录但没有充值
                }
                Response.Redirect(string.Format("login.aspx?ErrorInfo={0}", resultMessage));
            }
            else
            {
                String ipAddress = Request.UserHostAddress;
                if (!users.LogLogin(users.GetUserId(name.Text), ipAddress))
                {
                    newError = String.Format("系统繁忙登录失败!");
                    url = String.Format("login.aspx?error={0}", newError);
                    Page.Response.Redirect(url);
                }
                Session["userName"] = name.Text;
                Session["allow"] = "allow";//成功登录并且充值卡在有效期
                FormsAuthentication.RedirectFromLoginPage(name.Text, true);
                Response.Redirect("ChargeSuccess.aspx");
            }
        }
    }
}

